<?php
class ApiStoreController extends ApiController
{

  public function dressupAction()
  {
    $store = $this->account->getStore();
    if ($this->req->isPost()) {
      $data = json_decode($this->req->getPostTrim('data'), true) ?? [] ;
      if (empty($data)) { return $this->errorReturn('参数错误'); }
      if (!$store && ($store = $this->account->openStore())) {
        //开店成功
        $this->account->bindParent($this->req->getPostTrim('parent_id'));
        $flowers = intval(0 . $this->getModel('config')->get('flowers.shore.open', 50));
        $this->account->chageFlowers('开店赠送', $flowers, '首次装扮店铺赠送鲜花');
      }
      if ($store) {
        $store->data = $data;
        $updateResutl = $store->update();
        return $this->okReturn(!!$updateResutl);
      }
    }
    return $this->errorReturn('参数错误');
  }

  public function unlockitemsAction()
  {
    if ($this->req->isPost()) {
      if ($tagetStore = $this->getModel('player')->getStore($this->req->getPostTrim('store_id'))) {
        if ($tagetStore->store_id == $this->account->player_id) {
          return $this->errorReturn('不能自己解锁自己');
        }
        if (!$tagetStore->isItemLocked()) {
          return $this->errorReturn('货架已解锁');
        }
        return $this->okReturn($tagetStore->unLockItem());
      }
    }
    return $this->errorReturn('该店铺还未开业喔');
  }

  //留言
  public function commentAction()
  {
    if ($this->req->isPost()) {
      $comment = $this->req->getPostTrim('comment');
      if (empty($comment)) {
        return $this->errorReturn('参数错误');
      } else if ((!$tagetStore = $this->getModel('player')->getStore($this->req->getPostTrim('store_id')))) {
        return $this->errorReturn('该店铺还未开业喔');
      } else if ($addResutl = !!$this->account->addComment($comment, $tagetStore->store_id)){
        $this->account->hudong('评论', "在店铺[{$tagetStore->nickname}]中发表评论");
      }
      return $this->okReturn($addResutl);
    } else {
      if (!($tagetStore = $this->getModel('player')->getStore($this->req->getQueryTrim('store_id')))) {
        return $this->okReturn([
          'list' => [],
          'like' => [],
        ]);
      }
      $page = max(intval($this->req->getQueryTrim('page', 1)), 1);
      $rows = intval($this->req->getQueryTrim('rows', 15));
      $list = $tagetStore->getCommentsList([], $page, $rows);
      $comments = [];
      foreach ($list as $comment) {
        $comments[] = $comment->comment_id;
      }
      return $this->okReturn([
        'list' => $list,
        'like' => $this->account->pickLikeComments($comments)
      ]);
    }
  }

  public function likeCommentAction()
  {
    if ($comment = $this->getModel('player')->getComment($this->req->getPostTrim('comment_id'))) {
      return $this->okReturn($comment->likeBy($this->account->player_id));
    }
    return $this->errorReturn('评论不存在！！');
  }

  public function itemCateAction()
  {
    $page = max(intval($this->req->getQueryTrim('page', 1)), 1);
    $rows = intval($this->req->getQueryTrim('rows', 15));
    $storeModel = $this->getModel('store');
    return $this->okReturn(['list' => $storeModel->getItemCatesList([
      'status' => ItemCate::IS_ONLINE
    ], $page, $rows)]);
  }

  //
  public function itemAction()
  {
    $page = max(intval($this->req->getQueryTrim('page', 1)), 1);
    $rows = min(intval($this->req->getQueryTrim('rows', 1)), 1);
    $storeModel = $this->getModel('store');
    $store = $this->account->getStore();
    if (!$store) { return $this->errorReturn( '您还未装扮店铺，请先装扮下店铺');}
    return $this->okReturn(['list' => $storeModel->getItemsList([
      'cate_id' => $this->req->getQueryTrim('cate_id'),
      'status' => Item::IS_ONLINE
    ], 1, 20)]);
  }

  public function itemUpAction()
  {
    $store = $this->account->getStore();
    if ($this->req->isPost()) {
      $itemids = $this->req->getPost('itemids');
      if (!is_array($itemids)) { return $this->errorReturn( '参数错误'); }
      if (!$store) { return $this->errorReturn( '您还未装扮店铺，请先装扮下店铺');}
      if (count($itemids) > $store->items) { return $this->errorReturn( '货架数不够！！'); }
      $upResult = $store->upItems($itemids);
      //出现猴子
      // if ($upResult && rand(0, 100) < intval($this->getModel('config')->get('monkey.appear.probability', 50))) {
        // $store->appearMonkey();
      // }
      return $this->okReturn([
        'upitems' => $upResult,
        'monkey' => boolval($store->monkey_appear),
      ]);
    }
    return $this->okReturn($store ? $store->getItems() : []);
  }

  public function delcommentAction()
  {
    $comment_id = $this->req->getPostTrim('comment_id');
    $playerModel = $this->getModel('player');
    $comment = $playerModel->getComment($comment_id);
    if (!$comment) {
      return $this->errorReturn('评论不存在');
    }
    if ($comment->player_id != $this->account->player_id && $comment->store_id != $this->account->player_id) {
      return $this->errorReturn('越权操作');
    }
    return $this->okReturn(boolval($comment->delete()));
  }

}